package com.example.demo.main.rabbitmq.message;

import com.example.demo.main.rabbitmq.utils.RabbitmqConnectionUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xieluoyin
 */
public class Producer {


    public static void main(String[] args) throws Exception {

        Connection connection = RabbitmqConnectionUtil.getConnection();

        //通过connection创建一个Channel
        Channel channel = connection.createChannel();

        Map<String, Object> headers = new HashMap<>();
        headers.put("my1", "111");
        headers.put("my2", "222");


        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                .deliveryMode(2)
                .contentEncoding("UTF-8")
                //十秒过期
                .expiration("10000")
                .headers(headers)
                .build();

        // 通过Channel发送数据
        for (int i = 0; i < 5; i++) {
            String msg = "Hello RabbitMQ!";
            //1 exchange   2 routingKey
            //默认交换机中，路由键与队列名相同的话会匹配上
            channel.basicPublish("", "test001", properties, msg.getBytes());
        }

        // 记得要关闭相关的连接
        channel.close();
        connection.close();
    }
}
